package com.breaker.kata.util;

import com.breaker.kata.leetcode.domain.ListNode;
import org.junit.platform.commons.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

public class ListNodeUtil {

    public static ListNode create(String data) {
        if (StringUtils.isBlank(data)) {
            return new ListNode();
        }
        String[] strings = data.split("_");
        ListNode listNode = null;
        for (int i = strings.length - 1; i >= 0; i--) {
            if (i == strings.length - 1) {
                listNode = new ListNode(Integer.parseInt(strings[i]));
            } else {
                listNode = new ListNode(Integer.parseInt(strings[i]), listNode);
            }
        }
        return listNode;
    }

    public static void assertEqual(ListNode result, ListNode excepted) {
        assertThat(platListNode(result)).isEqualTo(platListNode(excepted));
    }

    private static List<Integer> platListNode(ListNode data) {
        List<Integer> result = new ArrayList<>();
        ListNode next = data;
        while (next != null) {
            result.add(next.val);
            next = next.next;
        }
        return result;
    }
}
